/*
 * ServicesMenu.java
 *
 * Created on 7 de Junho de 2007, 15:10
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.view;

import com.EntertainerMidlet;
import com.ValueObjects.Filme;
import com.connection.HttpConstants;
import com.connection.HttpTop10;
import com.view.components.CanvasColor;
import com.view.components.CanvasForm;
import com.view.components.CanvasListGroup;
import com.view.components.CanvasListItem;
import com.view.components.ImageUtil;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author $Leandro$
 */
public class ServicesMenu extends CanvasForm implements Runnable {
    
    CanvasListGroup menuList;
    
    Image topImg = null, topImgSel = null;
    
    Image searchImg = null;
    
    EntertainerMidlet midlet;
    
    private Image imgTitle;
    
    private int[] rawInt;
    
    private Image imgSel;
    
    private boolean mTrucking = true;
    
    private boolean mUpdate = true;
    
    private boolean firstTime = true;
    
    private int mAlpha = 0;
    
    private int mValue = 5;
    
    private Image mBufferImage;
    
    private Graphics mGraphics;
    
    private Graphics g;
    
    Thread t;
    
    /**
     * Creates a new instance of ServicesMenu
     */
    public ServicesMenu(EntertainerMidlet midlet) {
        super(midlet);
        
        this.midlet = midlet;
        
        this.addOkAndBack();
        this.setCommandListener(this);
        
        try {
            //this.backgroundImg = Image.createImage("/imgs/background2.png");
            this.backgroundImg = Image.createImage("/imgs/background.png");
            topImg = Image.createImage("/imgs/top10.png");
            topImgSel = Image.createImage("/imgs/top10_selected.png");
            searchImg = Image.createImage("/imgs/search.png");
            imgSel = Image.createImage("/imgs/search_selected.png");
            
            imgTitle = Image.createImage("/imgs/title.png");
            
            // Load images. Use mGraphics to draw on the backbuffer.
            mBufferImage = Image.createImage(imgTitle.getWidth(), imgTitle.getHeight());
            mGraphics = mBufferImage.getGraphics();
            
            // Allocate mamory for the image array.
            // Use the getRGB method to get all ARGB values from the image to the rawInt array.
            rawInt = new int[imgTitle.getWidth() * imgTitle.getHeight()];
            imgTitle.getRGB(rawInt, 0, imgTitle.getWidth(), 0, 0, imgTitle.getWidth(), imgTitle.getHeight());
            
            t = new Thread(this);
            t.start();
            
            menuList = new CanvasListGroup(null,30,imgTitle.getHeight() + 10,false);
            
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public CanvasListGroup getMenuList() {
        return menuList;
    }
    protected void paint(Graphics g) {
        this.g = g;
        
        g.drawImage(this.backgroundImg,0,0,g.TOP|g.LEFT);
        
        g.drawImage(mBufferImage, 0, 0, g.TOP|g.LEFT);
        
        mUpdate = true;
        
        menuList.g = g;
        menuList.width = getWidth();
        
        if(menuList.items == null) {
            CanvasListItem itemTop = new CanvasListItem(g,"",g.TOP|g.LEFT,new CanvasColor(0x000000),new CanvasColor(0xFFFFFF));
            
            itemTop.imgIcon = topImg;
            itemTop.imgIconSelected = topImgSel;
            itemTop.id = MenuConstants.TOP10;
            
            menuList.addItem(itemTop);
            
            CanvasListItem itemBusca = new CanvasListItem(g,"",g.TOP|g.LEFT,new CanvasColor(0x000000),new CanvasColor(0xFFFFFF));
            
            itemBusca.imgIcon = searchImg;
            itemBusca.imgIconSelected = imgSel;
            itemBusca.id = MenuConstants.SEARCH;
            menuList.addItem(itemBusca);
        }
        
        menuList.paint();
    }
    
    protected void keyPressed(int keyCode) {
        String key = getKeyName(keyCode);
        menuList.keyPressedList(key);
        
        if(key.equals("SELECT")) {
            this.commandAction(cmdOk,this);
        }
        
        repaint();
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        if(cmd == cmdOk) {
            if(menuList.selectedItem.id == MenuConstants.TOP10) {
                HttpTop10 http = HttpTop10.getInstance(HttpConstants.BUSCA_TOP10_URL,this);
                http.doPost(null);
            } else {
                if(menuList.selectedItem.id == MenuConstants.SEARCH) {
                    midlet.regiaoForm = new RegiaoForm(midlet);
                    t.yield();
                    midlet.display.setCurrent(midlet.regiaoForm);
                }
            }
        }
        
        if(cmd == cmdBack){
            midlet.display.setCurrent(midlet.formMenu);
        }
    }
    
    public void goTop10(DataInputStream dataIn, int itNum) {
        t.yield();
        midlet.formTop = new RankingForm(midlet,Filme.getVectorOfFilmes(dataIn,itNum));
        midlet.display.setCurrent(midlet.formTop);
    }
    
    public void goSearch() {
        t.yield();
        midlet.formSearch = new SearchForm(midlet);
        
        midlet.display.setCurrent(midlet.formSearch);
    }
    
    
    public void run() {
        while(mTrucking){
            if(mUpdate) {
                // change the alpha value each loop. 255=opaque, 0=transparent.
                mAlpha+= mValue;
                
                if(mAlpha>=255) {
                    //mValue = mValue *-1;
                } else {
                    if(mAlpha<=0) {
                        mValue = mValue *-1;
                    }
                }
                
                // Use the blend method in ImageEffect the change the Alpha value.
                // create a new Image from the new rawInt array.
                ImageUtil.blend(rawInt, mAlpha);
                Image fadingImage = Image.createRGBImage(rawInt, imgTitle.getWidth(), imgTitle.getHeight(), true);
                
                mGraphics.setColor(0xFEFED8);
                //mGraphics.fillRect(0, 0, getWidth(), getHeight());
                mGraphics.drawImage(fadingImage, 0, 0, 0);
                
                mUpdate = false;
            }
            repaint();
        }
    }
}